package notes.java.java8.demo;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

// 新时间
// LocalDate localTime LocalDateTime 用法相似LocalDateTime包含LocalDate和LocalTime
public class K11_TestNewTime2 {
	
	public static void main(String[] args) {
		// LocalDate localTime LocalDateTime
		// 1、LocalDate
		LocalDate ld = LocalDate.now();
		System.out.println(ld.getYear() + "-" + ld.getMonth() + "-" + ld.getDayOfMonth());
		// 2、LocalTime
		LocalTime lt = LocalTime.now();
		System.out.println(lt.getHour() + ":" + lt.getMinute() + ":" + lt.getSecond());;
		// 3、LocalDateTime
		LocalDateTime ldt = LocalDateTime.now();
		System.out.println(ldt.getYear() + "-" + ldt.getMonth() + "-" + ldt.getDayOfMonth() + " " + ldt.getHour() + ":" + ldt.getMinute() + ":" + ldt.getSecond());
		
		// 这三个用法是类似的
		LocalDateTime ldt2 = LocalDateTime.of(2017, 2, 10, 11, 25, 45, 234);
		System.out.println(ldt2.getYear() + "-" + ldt2.getMonth() + "-" + ldt2.getDayOfMonth() + " " + ldt2.getHour() + ":" + ldt2.getMinute() + ":" + ldt2.getSecond());
		
		// 加2年3月2天
		LocalDateTime ldt3 = ldt2.plusYears(2).plusMonths(3).plusDays(2);
		System.out.println(ldt3.getYear() + "-" + ldt3.getMonth() + "-" + ldt3.getDayOfMonth() + " " + ldt3.getHour() + ":" + ldt3.getMinute() + ":" + ldt3.getSecond());
		
		// 减1年1月2天 2小时20分钟10秒
		LocalDateTime ldt4 = ldt3.minusYears(1).minusMonths(1).minusDays(2).minusHours(2).minusMinutes(20).minusSeconds(10);
		System.out.println(ldt4.getYear() + "-" + ldt4.getMonth() + "-" + ldt4.getDayOfMonth() + " " + ldt4.getHour() + ":" + ldt4.getMinute() + ":" + ldt4.getSecond());
		
		System.out.println("----------------------");
		
		// 4、Instant 时间戳
		Instant inst = Instant.now();
		System.out.println(inst); // 默认是UTC时区
		ZonedDateTime inst2 = inst.atZone(ZoneId.systemDefault()); // 系统本地
		System.out.println(inst2);
		ZonedDateTime inst3 = inst2.plusHours(2);
		System.out.println(inst3);
		
		// 5、Duration 计算时间间隔
		Duration duration = Duration.between(inst2, inst3);
		System.out.println(duration.toHours()); // 相差小时
		
		// 6、Period 计算日期间隔
		LocalDate start = LocalDate.now();
		LocalDate end  = LocalDate.of(2017, 4, 23);
		Period p = Period.between(start, end);
		System.out.println(p.getDays()); // 相差天数
		
		// 7、TemporalAdjuster 时间校正器 有时要指定一些特殊的时间，如把时间调整到下个周日
		LocalDateTime ldtx = LocalDateTime.now();
		LocalDateTime nextSunday = ldtx.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
		System.out.println(nextSunday);
		
		// 8、DateTimeFormatter 格式化时间
		DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
		String date = ldtx.format(dtf);
		System.out.println(date);
		// 自定义
		DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
		String date2 = ldtx.format(dtf2);
		System.out.println(date2);

		// 9、ZoneDate ZoneTime ZoneDateTime 时区操作
		// 获取所有支持的时区
		ZoneId.getAvailableZoneIds().stream().forEach(System.out::println);
		LocalDateTime now = LocalDateTime.now();
		ZonedDateTime ldtTokyo = now.atZone(ZoneId.of("Asia/Tokyo")); // 东京
		LocalDateTime ldtTokyo2 = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
		System.out.println(ldtTokyo);
		System.out.println(ldtTokyo2); // 比上面少了+09:00[Asia/Tokyo] 与UTC时间有9个小时时差
	}

}
